/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template  
 */
package com.dwusoft.platform.template;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.sf.json.JSONObject;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.client.UctCoreClient;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.entity.User;

/**
 * description:  对接用户中心标签
 * @author Hou Dayu 创建时间：2015-12-29  
 * 
 */
@SuppressWarnings("serial")
public class UcClientTag extends TagSupport {

	private String doCode;//枚举代码
	private String pageSize;//记录数
	private String sqlQuery;//查询条件
	private String attrName;//属性名

	public int doStartTag() throws JspException {
		boolean b = true;
		try {
			HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
			LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
			if ("usersByDeptId".equals(doCode)) {//获取当前用户所在部门下的用户列表
				if (Validator.isNotNull(loginUser.getDeptId())) {
					Map<String, Object> params = new HashMap<String, Object>();
					params.put("deptId", loginUser.getDeptId());
					ResultEntity entity = UctCoreClient.searchUserByReflect(params);
					if ("true".equals(entity.getResult()) && !Validator.isEmpty(entity.getEntity())) {
						List<User> list = (List<User>) entity.getEntity();
						request.setAttribute(Validator.getDefaultStr(attrName, "userList"), list);
					}
				}
			}
		} catch (Exception e) {
		}
		if (b) {
			return super.EVAL_BODY_INCLUDE;
		} else {
			return super.SKIP_BODY;
		}
	}

	//	public int doEndTag() {
	//		return super.EVAL_BODY_INCLUDE;
	//	}

	/**
	 * json转map
	 */
	private void json2map1(Map<String, Object> params, HttpServletRequest request) {
		if (StrUtil.isNotNull(sqlQuery)) {
			JSONObject jsonObject = JSONObject.fromObject(sqlQuery);
			Map<String, Object> map1 = JSONObject.fromObject(jsonObject);
			params.putAll(map1);
		}
	}

	public String getDoCode() {
		return doCode;
	}

	public void setDoCode(String doCode) {
		this.doCode = doCode;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getSqlQuery() {
		return sqlQuery;
	}

	public void setSqlQuery(String sqlQuery) {
		this.sqlQuery = sqlQuery;
	}

	public String getAttrName() {
		return attrName;
	}

	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}
}
